home *** CD-ROM | disk | FTP | other *** search
- /* ------------------------------------------------------------ */
- /*
- HTTrack Website Copier, Offline Browser for Windows and Unix
- Copyright (C) Xavier Roche and other contributors
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
- Important notes:
-
- - We hereby ask people using this source NOT to use it in purpose of grabbing
- emails addresses, or collecting any other private information on persons.
- This would disgrace our work, and spoil the many hours we spent on it.
-
-
- Please visit our Website: http://www.httrack.com
- */
-
-
- /* ------------------------------------------------------------ */
- /* File: Interface */
- /* Author: Xavier Roche */
- /* ------------------------------------------------------------ */
-
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include "htsglobal.h"
- #include "htsbase.h"
-
- #include "HTTrackInterface.h"
-
- // Lecture d'une ligne (peut Ωtre unicode α priori)
- int linput(FILE* fp,char* s,int max) {
- int c;
- int j=0;
- do {
- c=fgetc(fp);
- if (c!=EOF) {
- switch(c) {
- case 13: break; // sauter CR
- case 10: c=-1; break;
- case 9: case 12: break; // sauter ces caractΦres
- default: s[j++]=(char) c; break;
- }
- }
- } while((c!=-1) && (c!=EOF) && (j<(max-1)));
- s[j]='\0';
- return j;
- }
- int linput_trim(FILE* fp,char* s,int max) {
- int rlen=0;
- char* ls=(char*) malloct(max+2);
- s[0]='\0';
- if (ls) {
- char* a;
- // lire ligne
- rlen=linput(fp,ls,max);
- if (rlen) {
- // sauter espaces et tabs en fin
- while( (rlen>0) && ((ls[max(rlen-1,0)]==' ') || (ls[max(rlen-1,0)]=='\t')) )
- ls[--rlen]='\0';
- // sauter espaces en dΘbut
- a=ls;
- while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
- a++;
- rlen--;
- }
- if (rlen>0) {
- memcpy(s,a,rlen); // can copy \0 chars
- s[rlen]='\0';
- }
- }
- //
- freet(ls);
- }
- return rlen;
- }
- int linput_cpp(FILE* fp,char* s,int max) {
- int rlen=0;
- s[0]='\0';
- do {
- int ret;
- if (rlen>0)
- if (s[rlen-1]=='\\')
- s[--rlen]='\0'; // couper \ final
- // lire ligne
- ret=linput_trim(fp,s+rlen,max-rlen);
- if (ret>0)
- rlen+=ret;
- } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
- return rlen;
- }
-
- // idem avec les car spΘciaux
- void rawlinput(FILE* fp,char* s,int max) {
- int c;
- int j=0;
- do {
- c=fgetc(fp);
- if (c!=EOF) {
- switch(c) {
- case 13: break; // sauter CR
- case 10: c=-1; break;
- default: s[j++]=(char) c; break;
- }
- }
- } while((c!=-1) && (c!=EOF) && (j<(max-1)));
- s[j++]='\0';
- }
-
- // Like linput, but in memory (optimized)
- int binput(char* buff,char* s,int max) {
- int count = 0;
- int destCount = 0;
-
- // Note: \0 will return 1
- while(count < max && buff[count] != '\0' && buff[count] != '\n') {
- if (buff[count] != '\r') {
- s[destCount++] = buff[count];
- }
- }
- s[destCount] = '\0';
-
- // then return the supplemental jump offset
- return count + 1;
- }
-
- int fexist(char* s) {
- struct stat st;
- memset(&st, 0, sizeof(st));
- if (stat(s, &st) == 0) {
- if (S_ISREG(st.st_mode)) {
- return 1;
- }
- }
- return 0;
- }
-
- INTsys fsize(char* s) {
- FILE* fp;
- fp=fopen(s,"rb");
- if (fp!=NULL) {
- INTsys i;
- fseek(fp,0,SEEK_END);
- i=ftell(fp);
- fclose(fp);
- return i;
- } else return -1;
- }
-
- TStamp time_local(void) {
- return ((TStamp) time(NULL));
- }
-
-
-
-
-
- #define NOSTATIC_RESERVE(name, type, nelt) NOSTATIC_XRESERVE(name, type, nelt)
- #define NOSTATIC_XRESERVE(name, type, nelt) do { \
- __declspec( thread ) static type thValue[nelt]; \
- __declspec( thread ) int static initValue = 0; \
- name = thValue; \
- if (!initValue) { \
- initValue = 1; \
- memset(&thValue, 0, sizeof(thValue)); \
- } \
- } while(0)
-
-
-
- typedef struct concat_strc {
- char buff[16][HTS_URLMAXSIZE*2*2];
- int rol;
- } concat_strc;
- char* concat(const char* a,const char* b) {
- concat_strc* strc;
- NOSTATIC_RESERVE(strc, concat_strc, 1);
- strc->rol=((strc->rol+1)%16); // roving pointer
- strcpybuff(strc->buff[strc->rol],a);
- if (b) strcatbuff(strc->buff[strc->rol],b);
- return strc->buff[strc->rol];
- }
- // conversion fichier / -> antislash
- #if HTS_DOSNAME
- char* __fconv(char* a) {
- int i;
- for(i=0;i<(int) strlen(a);i++)
- if (a[i]=='/') // convertir
- a[i]='\\';
- return a;
- }
- char* fconcat(char* a,char* b) {
- return __fconv(concat(a,b));
- }
- char* fconv(char* a) {
- return __fconv(concat(a,""));
- }
- #endif
-
- /* / et \\ en / */
- char* __fslash(char* a) {
- int i;
- for(i=0;i<(int) strlen(a);i++)
- if (a[i]=='\\') // convertir
- a[i]='/';
- return a;
- }
- char* fslash(char* a) {
- return __fslash(concat(a,""));
- }
-
- // conversion minuscules, avec buffer
- char* convtolower(char* a) {
- concat_strc* strc;
- NOSTATIC_RESERVE(strc, concat_strc, 1);
- strc->rol=((strc->rol+1)%16); // roving pointer
- strcpybuff(strc->buff[strc->rol],a);
- hts_lowcase(strc->buff[strc->rol]); // lower case
- return strc->buff[strc->rol];
- }
-
- // conversion en minuscules
- void hts_lowcase(char* s) {
- int i;
- for(i=0;i<(int) strlen(s);i++)
- if ((s[i]>='A') && (s[i]<='Z'))
- s[i]+=('a'-'A');
- }
-
- char* next_token(char* p,int flag) {
- int detect=0;
- int quote=0;
- p--;
- do {
- p++;
- if (flag && (*p=='\\')) { // sauter \x ou \"
- if (quote) {
- char c='\0';
- if (*(p+1)=='\\')
- c='\\';
- else if (*(p+1)=='"')
- c='"';
- if (c) {
- char* tempo = malloc(strlen(p) + 2 + 2);
- tempo[0]=c; tempo[1]='\0';
- strcatbuff(tempo,p+2);
- strcpybuff(p,tempo);
- free(tempo);
- }
- }
- }
- else if (*p==34) { // guillemets (de fin)
- char* tempo = malloc(strlen(p) + 2);
- tempo[0]='\0';
- strcatbuff(tempo,p+1);
- strcpybuff(p,tempo); /* wipe "" */
- p--;
- /* */
- quote=!quote;
- /* */
- free(tempo);
- }
- else if (*p==32) {
- if (!quote)
- detect=1;
- }
- else if (*p=='\0') {
- p=NULL;
- detect=1;
- }
- } while(!detect);
- return p;
- }
-